package com.qingpeng.pay.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.qingpeng.pay.R;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseActivity;
import com.qingpeng.pay.beans.NullBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.APP;
import com.qingpeng.pay.utils.StringUtils;
import com.qingpeng.pay.utils.TimerCountUtils;
import com.qingpeng.pay.utils.ToastUtils;
import com.qingpeng.pay.views.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class VerifyCodeActivity extends BaseActivity<NullBean> {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.btn_ok)
    Button btnOk;
    private CountDownTimer timer;
    Intent intent;
    int type;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_verify_code;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        intent = getIntent();
        type = intent.getIntExtra("type", 1);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VerifyCodeActivity.this.finish();
            }
        });
        String title = "";
        if (type == 1) {
            title = "设置支付密码";
        } else {
            title = "重置支付密码";
        }
        titleBar.setTitle(title);
        tvPhone.setText(APP.userInfo.getAccount());
    }

    @OnClick({R.id.tv_code, R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_code:
                if (!TimerCountUtils.status) {
                    timer = TimerCountUtils.getTimer(tvCode);
                    timer.start();
                    call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).sendzfcode();
                    call.enqueue(new Callback<NullBean>() {
                        @Override
                        public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                            NullBean body = response.body();
                            if (body.getStatus() != 1) {
                                ToastUtils.showToast(VerifyCodeActivity.this,  body.getInfo());
                                return;
                            }
                            ToastUtils.showToast(VerifyCodeActivity.this, body.getInfo());
                        }

                        @Override
                        public void onFailure(Call<NullBean> call, Throwable t) {
                            ToastUtils.showToast(VerifyCodeActivity.this, "网络请求错误");
                        }
                    });
                }
                break;
            case R.id.btn_ok:
                final String s = etCode.getText().toString();
                if (StringUtils.isEmpty(s)) {
                    ToastUtils.showToast(VerifyCodeActivity.this, "请输入验证码");
                    return;
                }
                call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).vrifyzfcode(s);
                call.enqueue(new Callback<NullBean>() {
                    @Override
                    public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                        NullBean body = response.body();
                        if (body.getStatus() != 1) {
                            ToastUtils.showToast(VerifyCodeActivity.this, body.getInfo());
                            return;
                        }
                        ToastUtils.showToast(VerifyCodeActivity.this, body.getInfo());
                        startActivity(PayPassowrdActivity.class, intent);
                        VerifyCodeActivity.this.finish();
                    }

                    @Override
                    public void onFailure(Call<NullBean> call, Throwable t) {
                        ToastUtils.showToast(VerifyCodeActivity.this, "网络请求错误,请稍后重试");
                    }
                });
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            TimerCountUtils.status = false;
            timer.cancel();
        }
    }
}
